var Flow = function(list) {
    var that = this;
    var currentItem = 0;
    var html = "<img style='display:inline;' src='site/resources/img/icons/ajax-loader.gif'><div style='display:inline;' id='flowmsg'></div>";
    var defaultMsg = "";
    var beforeStart = function() { return true; };
    var afterFinish = function() { };

    this.executeFlow = function() {
        currentItem = 0;
        openOverlay(300, 50, html);
        jQuery("div#flowmsg").html(defaultMsg);
        doFlowItem();
    }

    this.onBeforeStart = function(callback) { beforeStart = callback; }
    this.onAfterFinish = function(callback) { afterFinish = callback; }

    this.setDefaultMessage = function(msg) {
        defaultMsg = msg;
    }

    var doFlowItem = function() {
        if (currentItem == 0) {
            if (!beforeStart()) { return; }
        }
        if (currentItem == list.length) {
            closeOverlay();
            afterFinish();
            return;
        }
        var item = list[currentItem];
        var cb = {
            success : function(result) {
                currentItem++;
                doFlowItem();
            },
            failure : function(errorCode, msg) {
                jQuery("div#flowmsg").addClass("error").text(msg);
                setTimeout(function() {
                    closeOverlay();
                }, 3000);
            }
        };
        jQuery("div#flowmsg").text(item.statusMessage);
        if (typeof(item.params) == "function") {
            item.params = item.params();
        }
        if (item.userFunction) {
            if (item.callback(item.params)) {
                currentItem++;
                doFlowItem();
            }
        } else {
            if (item.admin) {
                if (item.html) {
                    callHtmlAdminService(item.page, item.service, item.params, cb);
                } else {
                    callAdminService(item.page, item.service, item.params, cb);
                }
            } else {
                if (item.html) {
                    callHtmlService(item.page, item.service, item.params, cb);
                } else {
                    callService(item.page, item.service, item.params, cb);
                }
            }
        }
    }
}

function adminService(message, pageName, serviceName, params) {
    return { statusMessage : message, userFunction : false, html : false, admin : true, page : pageName, service : serviceName, params : params};
}

function normalService(message, pageName, serviceName, params) {
    return { statusMessage : message, userFunction : false, html : false, admin : false, page : pageName, service : serviceName, params : params};
}

function htmlService(message, pageName, serviceName, params) {
    return { statusMessage : message, userFunction : false, html : true, admin : false, page : pageName, service : serviceName, params : params};
}

function htmlAdminService(message, pageName, serviceName, params) {
    return { statusMessage : message, userFunction : false, html : true, admin : true, page : pageName, service : serviceName, params : params};
}

function userFunction(message, callback, params) {
    return { statusMessage : message, html : false, admin : false, userFunction : true, callback : callback, params : params};
}
